package http

import (
	"encoding/json"
	"fmt"
)

type Response[T any] struct {
	Code    int16
	Message string
	Data    T
}

func OK() Response[string] {
	s := Response[string]{
		Code:    0,
		Message: "success",
		Data:    "",
	}
	return s
}
func OKWithMessage(message string) Response[string] {
	s := Response[string]{
		Code:    0,
		Message: message,
		Data:    "",
	}
	return s
}

func OKWithData[T any](data T) Response[T] {
	s := Response[T]{
		Code:    0,
		Message: "success",
		Data:    data,
	}
	return s
}

//// 为 Response[string] 类型定义的 String 方法
//func (r Response[string]) String() string {
//	return fmt.Sprintf("Code: %d, Message: %s, Data: %s", r.Code, r.Message, r.Data)
//}

// 为泛型 Response[T] 类型定义的 String 方法
func (r Response[T]) String() string {
	// 使用 json.Marshal 将结构体转换为 JSON 字符串
	jsonData, err := json.Marshal(r)
	if err != nil {
		return fmt.Sprintf("Error marshaling to JSON: %v", err)
	}
	return string(jsonData)
}
